<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="出入库模块名" prop="partName" label-width="120px">
        <el-input
          v-model="queryParams.partName"
          placeholder="请输入出入库模块名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:audit:remove']"
        >审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
-->

    <el-table v-loading="loading" :data="auditList" @selection-change="handleSelectionChange">
      <!--<el-table-column label="项目编号" align="center" prop="projectId"/>-->
      <el-table-column label="出入库模块名" align="center" prop="partName" />
      <el-table-column label="仓库" align="center" prop="depotName" />
      <el-table-column label="商品" align="center" prop="inventoryName" />
      <el-table-column label="修改前产品数量" align="center" prop="beforeNum" />
      <el-table-column label="修改后产品数量" align="center" prop="afterNum" />
      <el-table-column label="修改前产品金额" align="center" prop="beforeMoney" />
      <el-table-column label="修改后产品金额" align="center" prop="afterMoney" />
      <el-table-column label="项目编号" align="center" prop="projectCode"/>
      <el-table-column label="修改备注" align="center" prop="remark" />

      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor"/>
      <el-table-column label="审核备注" align="center" prop="auditRemark"/>
      <el-table-column label="修改时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="creator"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['warehouse:audit:remove']"
          >审核</el-button><!--
        </template>
        <template slot-scope="scope">-->
          <el-button
            v-if="scope.row.auditStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-cancel"
            @click="handleCancel(scope.row)"
            v-hasPermi="['warehouse:audit:cancel']"
          >作废</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出入库审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品id" prop="inventoryId">
          <el-input v-model="form.inventoryId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="仓库id" prop="depotId">
          <el-input v-model="form.depotId" placeholder="请输入仓库id" />
        </el-form-item>
        <el-form-item label="修改前产品数量" prop="beforeNum">
          <el-input v-model="form.beforeNum" placeholder="请输入修改前产品数量" />
        </el-form-item>
        <el-form-item label="修改后产品数量" prop="afterNum">
          <el-input v-model="form.afterNum" placeholder="请输入修改后产品数量" />
        </el-form-item>
        <el-form-item label="出入库记录id" prop="depotitemId">
          <el-input v-model="form.depotitemId" placeholder="请输入出入库记录id" />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.audit_status"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditDate">
          <el-date-picker clearable
            v-model="form.auditDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改前产品金额" prop="beforeMoney">
          <el-input v-model="form.beforeMoney" placeholder="请输入修改前产品金额" />
        </el-form-item>
        <el-form-item label="修改后产品金额" prop="afterMoney">
          <el-input v-model="form.afterMoney" placeholder="请输入修改后产品金额" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出入库模块名" prop="partName">
          <el-input v-model="form.partName" placeholder="请输入出入库模块名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudit, getAudit, cancelAudit, addAudit, updateAudit } from "@/api/warehouse/audit";
import request from "../../../utils/request";
import {delWareCurrentStock} from "@/api/warehouse/WareCurrentStock";

export default {
  name: "Audit",
  dicts: ['audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库审核表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditStatus: null,
        partName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出入库审核列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.auditList)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inventoryId: null,
        depotId: null,
        beforeNum: null,
        afterNum: null,
        depotitemId: null,
        auditStatus: null,
        auditDate: null,
        beforeMoney: null,
        afterMoney: null,
        createDate: null,
        partName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出入库审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAudit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const ids = row.id || this.ids

      this.$modal.confirm('是否确认审核当前记录？').then(function() {
        return   updateAudit(ids);
      }).then(() => {
        this.$modal.msgSuccess("审核成功");
        this.getList();
      }).catch(() => {
        this.getList();
      });
    },
    /** 作废按钮操作 */
    handleCancel(row) {
      this.reset();

      this.$modal.confirm('是否确认作废当前记录？').then(function() {
        return   cancelAudit(row);
      }).then(() => {
        this.$modal.msgSuccess("作废成功");
        this.getList();
      }).catch(() => {
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehouse/audit/export', {
        ...this.queryParams
      }, `audit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
